package com.zhijian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhijian.entity.Note;
import com.zhijian.mapper.NoteMapper;
import com.zhijian.service.INoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("noteService")
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements INoteService {

    @Autowired
    private NoteMapper noteMapper;

    @Override
    public List<Note> list(Map<String, Object> map) {
        // 计算分页起始位置
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        if (pageNum != null && pageSize != null) {
            map.put("start", (pageNum - 1) * pageSize);
        }
        return noteMapper.list(map);
    }

    @Override
    public Long getTotal(Map<String, Object> map) {
        return noteMapper.getTotal(map);
    }

    @Override
    public void add(Note note) {
        note.setCreateTime(new Date());
        note.setStatus(0); // 设置初始状态为正常
        note.setViews(0);  // 初始化浏览量
        note.setComments(0); // 初始化评论数
        this.save(note);
    }

    @Override
    public void update(Note note) {
        this.updateById(note);
    }
} 